<?php
	
	include 'dbfields.php';

# this is the only place where anything specifying database fields--and how they are displayed--is referenced in the website.
$fields = array(new field ("pdbcode", "PDB Code"), 
				new field("ddgavg", "delta-delta-G average"), 
				new field("secstruct", "Secondary Structure") );

>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
function loadXMLDoc()
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("resultstable").innerHTML=xmlhttp.responseText;
		}
	}
	
	
	querystring = "";
	var inputs = new Array();
	for (var i = 0; inputs.push(document.getElementById("input"+i)); i++);
	
	for (i = 0; i < inputs.length; i+=3) {
		// arguments to this function come in tuples
		// of criterion, relation, value.
		// i.e. constraints like
		// pdbcode, equals, 3SL9
		tuplenum = (i/3)+1; // 1, 2, 3 instead of 0, 3, 6

		querystring += "crit" + tuplenum + "=" + input[i];
		querystring += "rel" + tuplenum + "=" + input[i+1];
		querystring += "val" + tuplenum + "=" + input[i+2];
	}

	xmlhttp.open("POST","query.php", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(querystring);
}
</script>
</head>
<body>
<?php
echo "
<script>\n
function add()\n
{\n
	// find the new id to add\n
	// how many input tags? that's the number of tuples\n
	// so multiply by 3, minus 1\n
	i = document.getElementById('addable').getElementsByTagName('input').length*3;\n\n
	
	oldhtml = document.getElementById('addable').innerHTML;\n
	var ip1 = i+1;\n
	var ip2 = i+2;\n
	var additional = '<select id=input' + i + ' name=input' + i + '>;\n\n";

foreach $field in $fields {
	echo "additional = additional + '<option value=" . $field->name . ">" . $field->alias . "</option>';\n";
}
	echo "additional = additional + '</select>';\n";
	echo "additional = additional + '<select id=input'+ip1+ 'name=input' + ip1 + '><option value=\"=\"" . 
	     ">equals</option><option value=\">\">greater than</option><option value=\"<\">less than</option></select>';\n";
	echo "additional = additional + '<input id=input'+ip2 + ' type=text name=input' + ip2 + ' value=\"\" /><br />';\n";
	
	echo "document.getElementById('addable').innerHTML = oldhtml + additional;\n
}\n\n

</script>\n";
	
>



<p>HeliDB</p><br />

Query: <br />

<form>
<?php
foreach $field in $fields {
	echo "<input type='checkbox'> name='fields' value='" . $field->name . "' />" . $field->alias . "PDB Code\n";
}
>
<input type="checkbox" name="ddgavg" value="Car" /> I have a car 
<div id="addable">
<select id='input0' name="input0">
<?php
foreach $field in $fields {
	echo "<option value='" . $field->name . "'>" . $field->alias . "PDB Code</option>\n";
}
>
</select>
<select id='input1' name="input1">
<option value='='>equals</option>
<option value='>'>greater than</option>
<option value='<'>less than</option>
</select>
<input  id='input2' type='text' name='input2' value='' /><br />
</div>
</form>
<input type='button' value='Add criterion' onClick='add()' />
<input type='submit' value='Get results' onclick='loadXMLDoc()' />



<table id=resultstable>
</table>

</body>
</html>